package com.ssm.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ssm.mapper.CollectMapper;
import com.ssm.pojo.collect.Collect;
import com.ssm.pojo.collect.CollectAddResponseParameter;
import com.ssm.pojo.collect.CollectDeleteResponseParameter;
import com.ssm.pojo.collect.CollectList;
import com.ssm.pojo.collect.CollectListResponseParameter;
import com.ssm.pojo.collect.CollectProduct;
import com.ssm.pojo.collect.CollectProductResponseParameter;
import com.ssm.service.CollectService;

@Service
public class CollectServiceImpl implements CollectService {
    @Autowired
    CollectMapper collectMapper;

	@Override
	public CollectAddResponseParameter add(Collect collect) {
		// TODO Auto-generated method stub
		Integer collectNum = collectMapper.collectNum(collect);
		// 响应参数
		CollectAddResponseParameter parameter = new CollectAddResponseParameter();
		if(collectNum.equals(0)) {
			Integer result = collectMapper.add(collect);
			if (result.equals(1)) {
				parameter.setErrorCode(0);
				parameter.setErrorMsg("收藏成功");
				parameter.setData(collect);
				return parameter;
			}
		}
		parameter.setErrorCode(1);
		parameter.setErrorMsg("收藏失败");
		parameter.setData(collect);
		return parameter;
	}

	@Override
	public CollectDeleteResponseParameter delete(int id) {
		// TODO Auto-generated method stub
		Integer result = collectMapper.delete(id);
		// 响应参数
		CollectDeleteResponseParameter parameter = new CollectDeleteResponseParameter();
		if (result.equals(1)) {
			parameter.setErrorCode(0);
			parameter.setErrorMsg("删除收藏成功");
			parameter.setData(id);
			return parameter;
		}
		parameter.setErrorCode(1);
		parameter.setErrorMsg("删除收藏失败");
		parameter.setData(id);
		return parameter;
	}

	@Override
	public CollectListResponseParameter list(int id) {
		// TODO Auto-generated method stub
		boolean collectFlag = true;
		List<CollectList> list = collectMapper.list(id);
		CollectProductResponseParameter parameter = new CollectProductResponseParameter();
		List<CollectProduct> products = new ArrayList<CollectProduct>();
		for(CollectList c:list) {
			if(collectFlag) {
				parameter.setId(c.getUserId());
				collectFlag = false;
			}
			CollectProduct product = new CollectProduct();
			product.setProductId(c.getProductId());
			product.setName(c.getName());
			product.setPrice(c.getPrice());
			product.setDefaultImg(c.getDefaultImg());
			products.add(product);
			parameter.setProduct(products);
		}
		CollectListResponseParameter wishList = new CollectListResponseParameter();
		wishList.setWishList(parameter);
		return wishList;
	}

}
